package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.AssetsStatusMapper;
import com.ly.heart.domain.AssetsStatusEntity;
import com.ly.heart.service.IAssetsStatusService;

import javax.annotation.Resource;

/**
 * 资产状态Service业务层处理
 *
 * @author cxl
 * @date 2024-09-03
 */
@Service
public class AssetsStatusServiceImpl extends ServiceImpl<AssetsStatusMapper,AssetsStatusEntity> implements IAssetsStatusService {

    @Resource
    private AssetsStatusMapper assetsStatusMapper;

    @Override
    public AjaxResult getAssetsStatusList(AssetsStatusEntity assetsStatus){
        List<AssetsStatusEntity> list = assetsStatusMapper.selectAssetsStatusEntityList(assetsStatus);
        Page<AssetsStatusEntity> page = new Page(assetsStatus.getPage(),assetsStatus.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,list));
    }

    /**
     * 查询资产状态
     * @param id 资产状态ID
     * @return 资产状态
     */
    @Override
    public AssetsStatusEntity selectAssetsStatusEntityById(String id)
    {
        return assetsStatusMapper.selectAssetsStatusEntityById(id);
    }

    /**
     * 查询资产状态列表
     *
     * @param assetsStatusEntity 资产状态
     * @return 资产状态
     */
    @Override
    public List<AssetsStatusEntity> selectAssetsStatusEntityList(AssetsStatusEntity assetsStatusEntity)
    {
        return assetsStatusMapper.selectAssetsStatusEntityList(assetsStatusEntity);
    }

    /**
     * 新增资产状态
     *
     * @param assetsStatus 资产状态
     * @return 结果
     */
    @Override
    public int insertAssetsStatusEntity(AssetsStatusEntity assetsStatus)
    {
        assetsStatus.setId(SnowflakeIdWorker.getInstance().nextStringId());
        assetsStatus.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsStatus.setCreateTime(DateUtils.getNowDate());
        return assetsStatusMapper.insertAssetsStatusEntity(assetsStatus);
    }

    /**
     * 修改资产状态
     *
     * @param assetsStatus 资产状态
     * @return 结果
     */
    @Override
    public int updateAssetsStatusEntity(AssetsStatusEntity assetsStatus)
    {
        assetsStatus.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsStatus.setUpdateTime(DateUtils.getNowDate());
        return assetsStatusMapper.updateAssetsStatusEntity(assetsStatus);
    }

    /**
     * 批量删除资产状态
     *
     * @param ids 需要删除的资产状态ID
     * @return 结果
     */
    @Override
    public int deleteAssetsStatusEntityByIds(String[] ids)
    {
        return assetsStatusMapper.deleteAssetsStatusEntityByIds(ids);
    }

    /**
     * 删除资产状态信息
     *
     * @param id 资产状态ID
     * @return 结果
     */
    @Override
    public int deleteAssetsStatusEntityById(String id)
    {
        return assetsStatusMapper.deleteAssetsStatusEntityById(id);
    }

    /**
     * 禁用/反禁用
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult disable ( String id ) {
        AssetsStatusEntity assetsStatus = this.getById(id);
        if ( assetsStatus == null ) {
            throw new CustomException( "数据不存在！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<AssetsStatusEntity>().
                set ( AssetsStatusEntity :: getIsDisabled, assetsStatus.getIsDisabled () == 1 ? 2 : 1 ).
                set ( AssetsStatusEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( AssetsStatusEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( AssetsStatusEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( assetsStatus.getIsDisabled () == 1 ? "禁用失败！" : "解禁失败！" );
        }
        if( assetsStatus.getIsDisabled () == 1 ){
            this.update(new LambdaUpdateWrapper<AssetsStatusEntity>().eq(AssetsStatusEntity::getId, id)
                    .set(AssetsStatusEntity::getUnbanBy, SecurityUtils.getLoginUser().getUser().getUserId())
                    .set(AssetsStatusEntity::getUnbanDept, CurrentUserUtil.getDeptId()));
        }
        if(assetsStatus.getIsDisabled() == 2){
            this.update(new LambdaUpdateWrapper<AssetsStatusEntity>().eq(AssetsStatusEntity::getId, id)
                    .set(AssetsStatusEntity::getDisableBy, SecurityUtils.getLoginUser().getUser().getUserId())
                    .set(AssetsStatusEntity::getDisableDept, CurrentUserUtil.getDeptId()));
        }
        return AjaxResult.success ( assetsStatus.getIsDisabled () == 1 ? "禁用成功" : "解禁成功" );
    }


}
